package com.wocai.jrt.orgadmin.pay.action;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vteba.web.action.GenericAction;
import com.vteba.web.action.JsonBean;
import com.wocai.jrt.orgadmin.employee.service.spi.EmployeeService;
import com.wocai.jrt.orgadmin.orders.bean.OrderDetailResBean;
import com.wocai.jrt.orgadmin.orders.model.Voucher;
import com.wocai.jrt.orgadmin.orders.service.spi.OrdersService;
import com.wocai.jrt.orgadmin.orders.service.spi.VoucherService;
import com.wocai.jrt.orgadmin.pay.model.AuditRecord;
import com.wocai.jrt.orgadmin.pay.model.AuditRecordBean;
import com.wocai.jrt.orgadmin.pay.service.spi.AuditRecordService;

/**
 * 订单审核记录控制器
 * @author yinlei
 * @date 2015-11-19 16:04:02
 */
@Controller
@RequestMapping("/auditRecord")
public class AuditRecordAction extends GenericAction<AuditRecord> {
	private static final Logger LOGGER = LoggerFactory.getLogger(AuditRecordAction.class);
	@Inject
	private AuditRecordService auditRecordServiceImpl;
	@Inject
	private VoucherService voucherServiceImpl;
	/**
	 * 获得订单审核记录List，Json格式。
	 * @param model 参数
	 * @return 订单审核记录List
	 */
	@ResponseBody
	@RequestMapping("/list")
	public List<AuditRecord> list(AuditRecord model) {
		try {
    		List<AuditRecord> list = auditRecordServiceImpl.queryList(model);
    		
    		if (LOGGER.isDebugEnabled()) {
    			LOGGER.debug("AuditRecord query success");
    		}
    		return list;
		} catch (Exception e) {
			LOGGER.error("AuditRecord query error, msg=[{}]", e.getMessage());
		}
		return Collections.emptyList();
	}
	
	/**
	 * 获得凭证List，Json格式。
	 * @param model 参数
	 * @return 订单审核记录List
	 */
	@ResponseBody
	@RequestMapping("/voucherList")
	public OrderDetailResBean voucherList(AuditRecord model) {
		OrderDetailResBean orderDetail = new OrderDetailResBean();
		try {
    		Voucher v = new Voucher();
			v.setOrderId(model.getOrderId());
    		List<Voucher> voucherList = voucherServiceImpl.queryList(v);
    		orderDetail.setVoucherList(voucherList);
    		orderDetail.setVoucherCount(voucherList.size());
    		if (LOGGER.isDebugEnabled()) {
    			LOGGER.debug("AuditRecord query success");
    		}
    		return orderDetail;
		} catch (Exception e) {
			LOGGER.error("AuditRecord query error, msg=[{}]", e.getMessage());
		}
		return orderDetail;
	}
	
	/**
     * 查询权限，以及初始化列表页面
     * @param auth 参数
     * @return 查询结果
     */
    @ResponseBody
    @RequestMapping("/query")
    public Map<String, Object> query(@RequestBody AuditRecord record) {
    	Map<String, Object> result = new HashMap<String, Object>();
    	try {
    		AuditRecordBean params = buildQuery(record);
    		List<AuditRecord> list = auditRecordServiceImpl.queryForList(params);
    		
    		// 统计的时候，不需要分页
    		params.setStart(null);
    		int totalRecord = auditRecordServiceImpl.count(params);
    		
    		if (LOGGER.isDebugEnabled()) {
    			LOGGER.debug("authorities query success.");
    		}
    		
    		// jQuery datatable 就是这样的参数，阿西吧
    		result.put("aaData", list); // 返回的数据
    		result.put("iTotalRecords", totalRecord); // 总记录数
    		result.put("iTotalDisplayRecords", totalRecord); // 显示的记录数
		} catch (Exception e) {
			LOGGER.error("authorities query error, msg=[{}]", e.getMessage());
		}
        return result;
    }
	
    private AuditRecordBean buildQuery(AuditRecord record) {
    	AuditRecordBean params = new AuditRecordBean();
    	
    	return params;
    }
    
	/**
     * 根据Id获得订单审核记录实体，Json格式。
     * @param id 参数id
     * @return 订单审核记录实体
     */
    @ResponseBody
    @RequestMapping("/get")
    public AuditRecord get(String id) {
    	AuditRecord model = null;
    	try {
    		model = auditRecordServiceImpl.get(id);
		} catch (Exception e) {
			LOGGER.error("get record error, errorMsg=[{}].", e.getMessage());
		}
        return model;
    }
	
//	/**
//     * 跳转到新增页面
//     * @return 新增页面逻辑视图
//     */
//    @RequestMapping("/add")
//    public String add() {
//        return "auditRecord/add";
//    }
    
    /**
     * 执行实际的审核操作
     * @param model 要新增的数据
     * @return 新增页面逻辑视图
     */
    @ResponseBody
    @RequestMapping("/doAdd")
    public JsonBean doAdd(@RequestBody AuditRecord model) {
    	JsonBean bean = new JsonBean();
    	bean = auditRecordServiceImpl.auditOrder(model);
    	return bean;
    }
}
